/*****************************************************************************
 * kal_harmonyos_sem.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "kal/source/kal_harmonyos/kal_harmonyos.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.sem"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_s8          name[PLUM_KAL_SEM_NAME_MAX];
    hi_u32           hdl;
    struct list_head list;
} kal_sem_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE kal_sem_t sem_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_s32 kal_sem_add(kal_sem_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            LOG_E("kal_sem_add param err");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_add(&node->list, &sem_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_sem_t *kal_sem_find(PLUM_CONST plum_s8 *name)
{
    struct list_head *pos;
    list_for_each(pos, &sem_head.list)
    {
        kal_sem_t *node = list_entry(pos, kal_sem_t, list);
        if (strcmp((const char *)name, (const char *)node->name) == 0) {
            return (node);
        }
    }

    return (plum_null);
}

PLUM_PRIVATE
plum_s32 kal_sem_del(kal_sem_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            break;
        }

        list_del(&node->list);

        plum_kal_mem_free(node);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_sem_t *kal_sem_apply(PLUM_CONST plum_s8 *name, hi_u32 hdl)
{
    kal_sem_t *node = plum_null;
    do {
        if (!sem_head.list.next) {
            INIT_LIST_HEAD(&sem_head.list);
        }

        node = kal_sem_find(name);
        if (!node) {
            node = (kal_sem_t *)plum_kal_mem_malloc(sizeof(kal_sem_t));
            if (!node) {
                LOG_E("kal_sem_apply malloc err");
                break;
            }

            snprintf((char *)node->name, sizeof(node->name), "%s", name);
            node->hdl = hdl;
        }
    } while (0);

    return (node);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_sem_create(const plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The sem name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (strlen((const char *)name) > PLUM_KAL_SEM_NAME_MAX) {
            LOG_E("The sem name is too long");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hi_u32 semid;
        rc = hi_sem_create(&semid, 1);
        if (rc) {
            LOG_E("hi_sem_create err,rc:%d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        kal_sem_t *node = kal_sem_apply(name, semid);
        if (!node) {
            LOG_E("kal_sem_apply err");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        rc = kal_sem_add(node);
        if (rc) {
            LOG_E("kal_sem_add err,rc:%d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_sem_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The sem name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_sem_t *node = kal_sem_find(name);
        if (!node) {
            LOG_E("not find the sem,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_sem_delete(node->hdl);
        if (rc) {
            LOG_E("hi_sem_delete err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        rc = kal_sem_del(node);
        if (rc) {
            LOG_E("kal_sem_del err,rc:%d", rc);
            break;
        }

        LOG_I("delete sem,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_sem_post(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The sem name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_sem_t *node = kal_sem_find(name);
        if (!node) {
            LOG_E("not find the sem,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_sem_signal(node->hdl);
        if (rc) {
            LOG_E("hi_sem_signal err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_sem_wait(PLUM_CONST plum_s8 *name, plum_u32 timeout)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The sem name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_sem_t *node = kal_sem_find(name);
        if (!node) {
            LOG_E("not find the sem,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = hi_sem_wait(node->hdl, timeout);
        if (rc) {
            LOG_E("hi_sem_wait err,rc:0x%08X", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_sem_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_sem_t *node = kal_sem_find(name);
        if (!node) {
            LOG_E("not find the sem,named:%s", name);
            return;
        }
        LOG_I("sem[%d] named:%s", node->hdl, node->name);
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &sem_head.list)
        {
            kal_sem_t *node = list_entry(pos, kal_sem_t, list);
            LOG_I("sem[%d] named:%s", node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
